package com.hesicare.cgm.service.impl;

import com.hesicare.cgm.entity.CgmEquipmentDTO;
import com.hesicare.health.entity.SysPatient;
import com.hesicare.health.mapper.cgm.CgmEquipmentMapper;
import com.hesicare.cgm.service.ICgmEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author 55097
 * @create 2021/12/6 14:55
 */
@Service
@Primary
public class CgmEquipmentServiceImpl implements ICgmEquipmentService {

    @Autowired
    private CgmEquipmentMapper cgmEquipmentMapper;


    /**
     * 新增cgm设备
     *
     * @param cgmSn cgm设备sn号
     * @return 插入记录数
     */
    @Override
    public synchronized int insertCgmEquipment(String cgmSn, SysPatient sysPatient) {
        Integer id = selectCgmEquipmentBySn(cgmSn);
        if (id == null) {
            CgmEquipmentDTO cgmEquipmentDTO = new CgmEquipmentDTO();
            cgmEquipmentDTO.setCgmSn(cgmSn);
            cgmEquipmentDTO.setBindingStep("230");
            cgmEquipmentDTO.setStatus("1");
            cgmEquipmentDTO.setPatientId(sysPatient.getPatientId());
            cgmEquipmentDTO.setCreateTime(new Date());
            return cgmEquipmentMapper.insertCgmEquipment(cgmEquipmentDTO);
        }
        return id;
    }

    @Override
    public Integer selectCgmEquipmentBySn(String cgmSn) {
        return cgmEquipmentMapper.selectCgmEquipmentByMac(cgmSn);
    }
}
